package com.metadata_system.ai.response;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.metadata_system.common.db.entity.ProjectNode;
import com.metadata_system.system.response.NodePropertiesItem;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class ProjectNodeResponse {
    private Integer id;
    private Integer projectId;
    private Integer metadataId;
    private Integer metadataNodeId;
    private String metadataNodeName;
    private List<NodePropertiesItem> metadataNodeProperties;
    private Integer total;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updatedTime;

    public static ProjectNodeResponse fromEntity(ProjectNode projectNode) {
        return ProjectNodeResponse.builder()
                .id(projectNode.getId())
                .projectId(projectNode.getProjectId())
                .metadataId(projectNode.getMetadataId())
                .metadataNodeId(projectNode.getMetadataNodeId())
                .metadataNodeName(projectNode.getMetadataNodeName())
                .metadataNodeProperties(JSON.parseArray(projectNode.getMetadataNodeProperties(), NodePropertiesItem.class))
                .total(projectNode.getDataTotal())
                .createdTime(projectNode.getCreatedTime())
                .updatedTime(projectNode.getUpdatedTime())
                .build();
    }

    public static List<ProjectNodeResponse> fromEntity(List<ProjectNode> projectNodes) {
        return projectNodes.stream().map(ProjectNodeResponse::fromEntity).collect(Collectors.toList());
    }
}
